import 'package:logger/logger.dart';
import 'package:qgchat/http/io.dart';
import 'package:qgchat/http/models/HotIdsModel.dart';
import 'package:qgchat/http/models/SearchModel.dart';
import 'package:qgchat/http/models/bannerModel.dart';
import 'package:qgchat/http/models/checkVersionModel.dart';
import 'package:qgchat/http/models/commentListModel.dart';
import 'package:qgchat/http/models/dongtaiInfoModel.dart';
import 'package:qgchat/http/models/dtListModel.dart';
import 'package:qgchat/http/models/flattererUserdayModel.dart';
import 'package:qgchat/http/models/mysenddongtaiModel.dart';
import 'package:qgchat/http/models/newanchorModel.dart';
import 'package:qgchat/http/models/recommendModel.dart';
import 'package:qgchat/http/models/resModel.dart';
import 'package:qgchat/http/models/textModel.dart';

class CommonApis {
  /// 轮播图
  static Future<List<BannerModel>> adpicture() async {
    Map result = await Io.get("index/adpicture");
    if (_checkResult(result)) {
      return result['data'].map<BannerModel>((item) {
        return BannerModel.fromJson(item);
      }).toList();
    } else {
      return List<BannerModel>();
    }
  }

  /// 主播推荐
  static Future<List<RecommendModel>> recommend(
      {int page, int limit = 4}) async {
    Map result =
        await Io.get("anchor/Recommend", data: {"page": page, "limit": limit});
    if (_checkResult(result)) {
      return result['data'].map<RecommendModel>((item) {
        return RecommendModel.fromJson(item);
      }).toList();
    } else {
      return List<RecommendModel>();
    }
  }

  /// 新人主播
  static Future<List<NewanchorModel>> newanchor(
      {int page, int limit = 6}) async {
    Map result =
        await Io.get("anchor/newanchor", data: {"page": page, "limit": limit});
    if (_checkResult(result)) {
      return result['data'].map<NewanchorModel>((item) {
        return NewanchorModel.fromJson(item);
      }).toList();
    } else {
      return List<NewanchorModel>();
    }
  }

  /// 搜索用户
  static Future search(String key) async {
    if (key.isEmpty) {
      return List<SearchModel>();
    }
    Map result =
        await Io.get("member/search_member", data: {'search_key': key});
    if (_checkResult(result)) {
      return result['data'].map<SearchModel>((item) {
        return SearchModel.fromJson(item);
      }).toList();
    } else {
      return List<SearchModel>();
    }
  }

  /// 与你有缘
  static Future fatefor() async {
    Map result = await Io.get("member/fatefor");
    if (_checkResult(result)) {
      return result['data'].map<SearchModel>((item) {
        return SearchModel.fromJson(item);
      }).toList();
    } else {
      return List<SearchModel>();
    }
  }

  /// 私信前检测主播状态
  static Future checkTalkStatus(token) async {
    return await Io.get("member/checkTalkStatus", data: {'token': token});
  }

  /// 与我亲密日榜
  static Future flattererUserday(String token) async {
    Map result =
        await Io.get("anchor/FlattererUserday", data: {"member_token": token});
    if (_checkResult(result)) {
      return result['data'].map<FlattererUserdayModel>((item) {
        return FlattererUserdayModel.fromJson(item);
      }).toList();
    } else {
      return List<FlattererUserdayModel>();
    }
  }

  /// 与我亲密周榜
  static Future flattererUserweek(String token) async {
    Map result =
        await Io.get("anchor/flattererUserweek", data: {"member_token": token});
    if (_checkResult(result)) {
      return result['data'].map<FlattererUserdayModel>((item) {
        return FlattererUserdayModel.fromJson(item);
      }).toList();
    } else {
      return List<FlattererUserdayModel>();
    }
  }

  /// 与我亲密总榜
  static Future flattererUser(String token) async {
    Map result =
        await Io.get("anchor/FlattererUser", data: {"member_token": token});
    if (_checkResult(result)) {
      return result['data'].map<FlattererUserdayModel>((item) {
        return FlattererUserdayModel.fromJson(item);
      }).toList();
    } else {
      return List<FlattererUserdayModel>();
    }
  }

  /// 检查应用版本
  static Future checkVersion(String version, int type) async {
    Map result = await Io.get("index/checkVersion", data: {
      "version": version,
      "type": type,
    });
    if (_checkResult(result)) {
      return CheckVersionModel.fromJson(result['data']);
    } else {
      return null;
    }
  }

  /// 文本检测
  static Future text(String text) async {
    Map result = await Io.post("index/text", data: {"text": text});
    if (_checkResult(result)) {
      return result['data'].map<TextModel>((item) {
        return TextModel.fromJson(item);
      }).toList();
    } else {
      return List<TextModel>();
    }
  }

  /// 同城主播
  /// cityname 为模糊搜索，可自由传省市区，现在为省，因为要考虑市区主播数量过少的问题
  static Future samecityanchor(String cityname,
      {int page = 1, int limit = 6}) async {
    Map result = await Io.get("anchor/samecityanchor", data: {
      "cityname": cityname,
      "page": page,
      "limit": limit,
    });
    if (_checkResult(result)) {
      return result['data'].map<NewanchorModel>((item) {
        return NewanchorModel.fromJson(item);
      }).toList();
    } else {
      return List<NewanchorModel>();
    }
  }

  /// 发布动态
  static Future senddongtai(String text,
      {List<String> img, String video}) async {
    final Map<String, dynamic> data = {"text": text};
    if (img != null) {
      data['img'] = img;
    }
    if (video != null) {
      data['video'] = video;
    }
    Map result = await Io.post("anchor/senddongtai", data: data);
    return ResModel.fromJson(result);
  }

  /// 我的动态
  static Future mysenddongtai({int page = 1, int limit = 6}) async {
    Map result = await Io.get("anchor/mysenddongtai", data: {
      "page": page,
      "limit": limit,
    });
    final list = List.generate(
      result['data'].length,
      (i) => MysenddongtaiModel.fromJson(result['data'][i]),
    );
    return list;
  }

  /// 删除动态
  static Future delsenddongtai(int id) async {
    Map result = await Io.get("anchor/delsenddongtai", data: {
      "id": id,
    });
    return ResModel.fromJson(result);
  }

  /// 动态详情
  static Future dongtaiInfo(int id) async {
    Map result = await Io.get("anchor/dongtaiInfo", data: {
      "id": id,
    });
    return DongtaiInfoModel.fromJson(result['data']);
  }

  /// 动态点赞
  static Future dtdz(int id) async {
    Map result = await Io.post("anchor/dtdz", data: {
      "follow_id": id,
    });
    return ResModel.fromJson(result);
  }

  /// 取消动态点赞
  static Future canceldtdz(int id) async {
    Map result = await Io.post("anchor/canceldtdz", data: {
      "follow_id": id,
    });
    return ResModel.fromJson(result);
  }

  /// 全世界动态列表
  static Future<List<DtListModel>> dtList({int page = 1, int limit = 6}) async {
    Map result = await Io.get("anchor/dtList", data: {
      "page": page,
      "limit": limit,
    });
    final list = List.generate(
      result['data'].length,
      (i) => DtListModel.fromJson(result['data'][i]),
    );
    return list;
  }

  /// 评论动态
  static Future sendDtComment(int id, String text, {int pid}) async {
    Map result = await Io.post("anchor/sendDtComment", data: {
      "id": id,
      "text": text,
      "pid": pid,
    });
    return ResModel.fromJson(result);
  }

  /// 动态评论列表
  static Future commentList(int id, {int page = 1, int limit = 9999}) async {
    Map result = await Io.get("anchor/commentList", data: {
      "id": id,
      "page": page,
      "limit": limit,
    });
    if (_checkResult(result)) {
      return result['data'].map<CommentListModel>((item) {
        return CommentListModel.fromJson(item);
      }).toList();
    } else {
      return List<CommentListModel>();
    }
  }

  /// 存放信鸽token
  static Future xingetoken(String xingetoken, String memberToken) async {
    Map result = await Io.post("member/xingetoken", data: {
      "xingetoken": xingetoken,
      "member_token": memberToken,
    });
    return ResModel.fromJson(result);
  }

  /// 获取排行榜\封禁账号
  static Future getCommonInfo(String api) async {
    Map result = await Io.get("anchor/" + api);
    return result;
  }

  /// 激活百度
  static Future baiduActive(String anid) async {
    return Io.get("baidu/active", data: {"android_id": anid});
  }

  /// 百度注册
  static Future baiduRegister(String anid) async {
    return Io.get("baidu/register", data: {"android_id": anid});
  }

  // 百度付费
  static Future baiduOrder(String anid, String price) async {
    return Io.get("baidu/orders", data: {"android_id": anid, "price": price});
  }

  /// 百度次日留存
  static Future baiduNextDay(String anid) async {
    return Io.get("baidu/retain_1day", data: {"android_id": anid});
  }

  /// 七牛获取token
  static Future qiniuToken() {
    return Io.get('qiniu/auth');
  }

  /// 主播推荐id列表
  static Future<List<HotIdsModel>> recommendIds() async {
    final res = await Io.get('anchor/recommendIds');
    return List.generate(
      res['data'].length,
      (i) => HotIdsModel.fromJson(res['data'][i]),
    );
  }

  /// 新主播推荐
  static Future recommendPage(List<int> ids) async {
    final result = await Io.get('anchor/recommendPage', data: {"ids": ids});
    List list = result['data'];
    return List.generate(
      list.length,
      (i) => RecommendModel.fromJson(list[i]),
    );
  }

  /// 新人主播所有id
  static Future<List<HotIdsModel>> newanchorIds() async {
    final res = await Io.get('anchor/newanchorIds');
    return List.generate(
      res['data'].length,
      (i) => HotIdsModel.fromJson(res['data'][i]),
    );
  }

  /// 新人主播分页查询
  static Future newanchorPage(List<int> ids) async {
    final result = await Io.get('anchor/newanchorPage', data: {"ids": ids});
    List list = result['data'];
    return List.generate(
      list.length,
      (i) => NewanchorModel.fromJson(list[i]),
    );
  }
}

/// 检测回调数据是否可用
_checkResult(res) {
  return res != null && res['status'] == 1;
}
